/*
 * PlaceWalls - wall placement phase of play
 *
 * Copyright 2007 James W. Hofmann
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#if flash9
import flash.Lib;
import flash.display.Bitmap;
import flash3f.InputHandler;
import flash3f.BitFont;
#else neko
import neko3f.InputHandler;
#end

class PlaceWalls
{

	// move puzzleshape drawing into HUD?

	public static var checkforspace : Bool;
	public static var shape : PuzzleShape;
	#if flash9
	static public var mypop : PopTart;
	public static var piecepic : Bitmap;
	public static var lastctx : Int;
	public static var lastcty : Int;
	public static var tutorialticker : Int;
	#end
	public static var pieces : Int;

	public static function start()
	{
		Gameplay.screenPlacement();
		FortressManager.calcterritory();
		InputHandler.clear();	
		shape = new PuzzleShape();
		pieces = 10;
		#if flash9
		tutorialticker = 0;
		Lib.current.addChild(shape);
		piecepic = new Bitmap();
		piecepic.bitmapData = BitFont.gen([pieces," LEFT"]);		
		Lib.current.addChild(piecepic);
		checkforspace = true;
		#end
	}

	public static function up()
	{
		input();
		
		#if flash9
			if (HUD.ctx!=lastctx || HUD.cty!=lastcty)
			shape.drawoutline();
			lastctx = HUD.ctx;
			lastcty = HUD.cty;
			tutorial();
		#end

		if (checkforspace)
		{
			if (FortressManager.cantfitpiece(shape))
			{
				shape.replace();
				pieces-=1;
				#if flash9
				piecepic.bitmapData = BitFont.gen([pieces," LEFT - LAST COULDN'T FIT!"]);				
				SFX.fortdeath = true;
				#end
			}
			else
			{
				checkforspace = false;
			}
		}
		
		if (pieces < 1)
		{
			ComponentManager.remove(PlaceWalls);
		}

	}

	public static function tutorial()
	{
		#if flash9
		if (tutorialticker==0)
		{
			mypop = new PopTart(["PLACE WALLS"]);
		}
		else if (tutorialticker==(30*4))
		{
			mypop = new PopTart(["Z, X, MOUSEWHEEL TO ROTATE"]);
		}
		else if (tutorialticker==(30*8))
		{
			mypop = new PopTart(["ADJACENT TO EXISTING FORTRESS OK"]);				
		}
		else if (tutorialticker==(30*12))
		{
			mypop = new PopTart(["SURROUND AREAS FOR TERRITORY"]);				
		}
		else if (tutorialticker==(30*16))
		{
			mypop = new PopTart(["ALL FORTRESS PIECES..."]);				
		}
		else if (tutorialticker==(30*20))
		{
			mypop = new PopTart(["COUNT AS TERRITORY"]);				
		}
		if (mypop!=null)
			mypop.waitforever = true;
		tutorialticker = (tutorialticker+1) % (30*24);
		#end
	}
	
	public static function input()
	{
		if (InputHandler.click)
		{			
			
			if (FortressManager.addshape(HUD.ctx, HUD.cty, shape))			
			{
				FortressManager.calcterritory();
				pieces-=1;
				#if flash9
				piecepic.bitmapData = BitFont.gen([pieces," LEFT"]);
				SFX.wallplacement = true;
				#end
				checkforspace = true;
			}

			
		}			
		
		if (InputHandler.wheeldelta==true || InputHandler.rotleftkey)
		{	
			shape.rotate();
			shape.rotate();
			shape.rotate();
			#if flash9
			lastctx = -9999;
			#end
		}
		else if (InputHandler.rotrightkey)
		{
			shape.rotate();
			#if flash9
			lastctx = -9999;
			#end
		}
	
	}
	
	public static function end()
	{
		#if flash9
		Lib.current.removeChild(shape);
		Lib.current.removeChild(piecepic);
		piecepic = null;
		mypop.waitforever = false;
		mypop.waittime = 0;
		#end
		shape = null;
		ComponentManager.add(WaveInterstitial);
	}

}
